package com.atguigu.atcrowdfunding.service.impl;

import com.atguigu.atcrowdfunding.bean.TAdminRoleExample;
import com.atguigu.atcrowdfunding.bean.TRole;
import com.atguigu.atcrowdfunding.bean.TRoleExample;
import com.atguigu.atcrowdfunding.bean.TRolePermissionExample;
import com.atguigu.atcrowdfunding.mapper.TAdminMapper;
import com.atguigu.atcrowdfunding.mapper.TAdminRoleMapper;
import com.atguigu.atcrowdfunding.mapper.TRoleMapper;
import com.atguigu.atcrowdfunding.mapper.TRolePermissionMapper;
import com.atguigu.atcrowdfunding.service.TRoleService;
import com.atguigu.atcrowdfunding.util.StringUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TRoleServiceImpl implements TRoleService {
    @Autowired
    TRoleMapper roleMapper;
    @Autowired
    TAdminRoleMapper adminRoleMapper;
    @Autowired
    TRolePermissionMapper rolePermissionMapper;

    @Override
    public PageInfo<TRole> listRolePage(Map<String,Object> paramMap) {
        String condition = (String)paramMap.get("condition");
        List<TRole> list = null;
        if(StringUtil.isEmpty(condition)){
            list = roleMapper.selectByExample(null);
        }else{
            TRoleExample example = new TRoleExample();
            example.createCriteria().andNameLike("%"+condition+"%");
            list = roleMapper.selectByExample(example);
        }

        PageInfo<TRole> page = new PageInfo<TRole>(list,5);
        return page;
    }

    @Override
    public void saveTRole(TRole role) {
        roleMapper.insertSelective(role);
    }

    @Override
    public TRole getRoleById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateTRole(TRole role) {
       roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public void deleteTRole(Integer id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TRole> listAllRole() {
        return roleMapper.selectByExample(null);
    }

    @Override
    public List<Integer> getRoleByAdminId(String id) {
        return adminRoleMapper.getRoleByAdminId(id);
    }

    @Override
    public void saveRoleAndAdminRelationship(Integer[] roleId, Integer adminId) {
        adminRoleMapper.saveRoleAndAdminRelationship(roleId,adminId);
    }

    @Override
    public void deleteRoleAndAdminRelationship(Integer[] roleId, Integer adminId) {

        adminRoleMapper.deleteRoleAndAdminRelationship(roleId,adminId);
    }

    @Override
    public void saveRoleAndPermissionRelationship(Integer roleId, List<Integer> ids) {
        //先删除之前分配的，然后分配所有打勾的
        TRolePermissionExample example = new TRolePermissionExample();
        example.createCriteria().andRoleidEqualTo(roleId);
        rolePermissionMapper.deleteByExample(example);
        rolePermissionMapper.saveRoleAndPermissionRelationship(roleId,ids);
    }

    @Override
    public List<Integer> listPermissionIdByRoleId(Integer roleId) {
        return rolePermissionMapper.listPermissionIdByRoleId(roleId);
    }
}
